package com.schedule;

import com.bean.ActJoinInfo;
import com.bean.Activity;
import com.bean.OffInfo;
import com.bean.vo.ActivityVo;
import com.controller.ActivityController;
import com.mapper.ActJoinInfoMapper;
import com.mapper.ActivityMapper;
import com.mapper.OffInfoMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class OffSetInfoSchedule {

    private Logger logger= LoggerFactory.getLogger(OffSetInfoSchedule.class);
    //定时任务，每分钟扫描一次，已经过期的活动，有没有报名没参加的，有，则加入到表里
    //已经扫描标记过的活动就不必了
    @Autowired
    private ActivityMapper activityMapper;

    @Autowired
    private OffInfoMapper offInfoMapper;


    @Autowired
    private ActJoinInfoMapper actJoinInfoMapper;

    //5分钟执行一次
    @Scheduled(cron = "* 0/5 * * * *")
    public void daylyOffSet(){
        logger.info("开始定时任务，查找放鸽子者");
        Activity activity=new Activity();
        activity.setDate(new Date());
        //查询没标记的并且过期的活动
      List<ActivityVo>activities= activityMapper.selectDateActicity(activity);
      //数据库设计标记扫描的字段，就是用来保证幂等
      if(activities.size()>0){
          logger.info("开始定时任务，查找放鸽子者，存在结束未标记的任务");
          activities.stream().forEach(e->{
                      ActJoinInfo actJoinInfo=new ActJoinInfo();
                      actJoinInfo.setActivityId(e.getId().toString());
                      //查询这些活动报名但是没签到的情况
                      List<ActJoinInfo>actJoinInfos=
                              actJoinInfoMapper.selectUnJoin(actJoinInfo);
                      if(actJoinInfos.size()>0){
                          logger.info("存在放鸽子者");
                          actJoinInfos.stream().forEach(
                                  actJoinInfo1 -> {
                                      OffInfo offInfo=new OffInfo();
                                      offInfo.setActivityId(actJoinInfo1.getActivityId());
                                      offInfo.setUserId(actJoinInfo1.getUserId());
                                      //把报名且未签到者记录下来
                                      offInfoMapper.insertSelective(offInfo);
                                      logger.info("存在放鸽子者，记录名单记录完毕");
                                  }
                          );
                      }

                  }
          );
      }

    //将没标记的过期的活动做上标记

    }
}
